package shiyan4;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class Client2 {
    public static void main(String[] args) throws IOException {
        int port1 = 8080;
        int port2 = 8081;
        DatagramSocket socket = new DatagramSocket(port1);
        socket.setBroadcast(true);



        while (true){
            byte[] buf = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);
            String msg = new String(packet.getData(), 0, packet.getLength());
            System.out.println("收到来自"+packet.getAddress().getHostAddress()+"的信息"+msg);

            String remsg="我在";
            byte[] buf1 = remsg.getBytes();
            DatagramPacket packet1 = new DatagramPacket(buf1, buf1.length, packet.getAddress(), packet.getPort());
            socket.send(packet1);
        }

    }
}
